package team.kas.anime.service.serviceImpl;

import it.sauronsoftware.jave.MultimediaInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import team.kas.anime.dao.CollectionMapper;
import team.kas.anime.dao.FanMapper;
import team.kas.anime.dao.VideoMapper;
import team.kas.anime.pojo.*;
import team.kas.anime.service.UserInfoService;
import team.kas.anime.util.UpUtils;
import team.kas.anime.util.VideoUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private CollectionMapper collectionMapper;
    @Autowired
    private FanMapper fanMapper;

    @Override
    public Result getAllVideos(Integer page,Integer limit,String userid) {
        Result result=new Result();
        if(page<=0)
        {
            page=1;
            page=(page-1)*limit;
        }
        else page=(page-1)*limit;
        List<Video> videos = videoMapper.selectAll(page,limit,userid);
        Integer count=videoMapper.getCount();
        result.setData(videos);
        result.setCount(count);
        return result;
    }

    @Override
    public Result del(String id) {
        Result result=new Result();
        Video video=videoMapper.selectByPrimaryKey(id);
        if (video!=null)
        {
            videoMapper.deleteByPrimaryKey(id);
            result.setMsg("true");
        }
        else
        {
            result.setCode(500);
            result.setMsg("error");
        }
        return result;
    }

    @Override
    @Transactional //添加事务
    //事务 做事时，要么全部成功，要么全部失败 如果出现异常，必须全部回滚
    public Result delete(String[] ids) {
        Result result=new Result();
        try {
            for (int i = 0; i < ids.length; i++)
            {
                videoMapper.deleteByPrimaryKey(ids[i]);
            }
            return result;
        }catch (Exception e)
        {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("error");
            return result;
        }

    }

    @Override
    public Result upVideo(MultipartFile file, HttpServletRequest request) {
        Result result=new Result();
        try {
            UpUtils.upfile1(file,request);
            String fileName= file.getOriginalFilename();
            result.setMsg(fileName);
            return result;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("操作失败");
            return result;
        }
    }

    @Override
    public Result upImg(MultipartFile file, HttpServletRequest request) {
        Result result=new Result();
        try {
            UpUtils.upImgFile(file,request);
            String fileName= file.getOriginalFilename();
            result.setMsg(fileName);
            return result;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("操作失败");
            return result;
        }
    }

    @Override
    public Result add(Video video, HttpServletRequest request,String uId) {
        //获取视频相关信息
        Result result=new Result();
        String id= UUID.randomUUID().toString();//自动生成id
        MultimediaInfo info = VideoUtil.video1(request, video.getVideoPath());
        try{
            long duration = info.getDuration();
            float videoSeconds=Float.valueOf(duration/1000);
            //使用构造函数 图片和视频的路径需不需要重新编辑路径
            Video video1=new Video(id,uId,video.getVideoDesc(),video.getVideoPath(),videoSeconds,video.getCoverPath(),1l,1,1l,0l, new Date(),video.getRemark());
            video1.setVideoDesc(video.getVideoDesc());
            String videoPath="/video/"+video.getVideoPath();
            video1.setVideoPath(videoPath);
            String imgPath="/img/"+video.getCoverPath();
            video1.setCoverPath(imgPath);
            videoMapper.insert(video1);
            result.setMsg("添加成功");
            return result;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("请求失败");
            return result;
        }
    }

    @Override
    public Result getAllColl(Integer page,Integer limit,String uid) {
        Result result=new Result();
        if(page<=0)
        {
            page=1;
            page=(page-1)*limit;
        }
        else page=(page-1)*limit;
        List<Collections> collections=collectionMapper.selectAll(page,limit,uid);
        Integer count=collectionMapper.getCount();
        result.setCount(count);
        result.setData(collections);
        return result;
    }

    @Override
    public Result delColl(String id) {
        Result result=new Result();
        try {
            collectionMapper.deleteByPrimaryKey(id);
            return result;
        }catch (Exception e)
        {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("error");
            return result;
        }
    }

    @Override
    public Result getAllFan(Integer page, Integer limit, String uid) {
        Result result=new Result();
        if(page<=0)
        {
            page=1;
            page=(page-1)*limit;
        }
        else page=(page-1)*limit;
        List<Fan> fans=fanMapper.selectAll(page,limit,uid);
        Integer count=fanMapper.getCount();
        result.setCount(count);
        result.setData(fans);
        return result;
    }

    @Override
    public Result delFan(String id) {
        Result result=new Result();
        try {
            fanMapper.deleteByPrimaryKey(id);
            return result;
        }catch (Exception e)
        {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("error");
            return result;
        }
    }
}
